<?php
namespace Home\Controller;
use Think\Controller;
class NewsController extends Controller {
   public function _initialize(){
	   if(session("username")){
		
			
		}else{
			  $this->redirect("Login/index"); 
		}
		$this->assign('index',4);
   }
   
   
  
   public function index(){
	   $news=D("news");
		
		if($_POST){
			
			
		}
		$where="type=2";
	
		 $count =$news->where($where)->count();
		 $Page= new \Think\Page($count,10);
		 $show= $Page->show();// 分页显示输出
		
		 $list=$news->where($where)->order('id DESC')->limit($Page->firstRow.','.$Page->listRows)->select();
		 
		
		  $this->assign('alist',$list);
		  $this->assign('page',$show);
	   layout('Layout/layout');
		$this->display();
   }
   
   
   public function add(){
	  $news=D("news");
	
		
		if($_POST){
			if($_POST["id"]){
				$data["id"] =$_POST["id"];
				$data["title"] =$_POST["title"];
				$data["content"] =$_POST["content"];
				$data["img"] = $_POST["img"];
				$data["createtime"] = time();
				
			
			
				
				if($news->save($data)){
					$this->success('修改成功','/index.php/Home/News/index');
					
				}else{
					$this->success('修改失败','/index.php/Home/News/index');
				}
			}else{
				$data["title"] =$_POST["title"];
				$data["content"] =$_POST["content"];
				$data["img"] = $_POST["img"];
				$data["type"] = "2";
				$data["uid"] = session("uid");
				$data["createtime"] = time();
				
				if($news->add($data)){
					$this->success('添加成功','/index.php/Home/News/index');
					
				}else{
					$this->success('添加失败','/index.php/Home/News/index');
				}
			}
			
		}else{
			if($_GET["id"]){
				
				
				
				$info =$news->where("id=".$_GET["id"])->find();
				
				$this->assign('info',$info);
			}else{
				
				
			}
			
			
		
			
		}
	   layout('Layout/layout');
		$this->display();
   }
	function delnews(){
		$ids =$_POST["ids"];
		 $model = D("news");//获取当期模块的操作对象
		 if(strpos($ids,",")){
			$where = 'id in('.$ids.')';
		  }else{
			$where = 'id='.$ids;
		  }
		   $list=$model->where($where)->delete();
		   if($list!==false) {
			  echo "0";
		   }else{
			  echo "-1";
		   }
		
	}
	
	


}